success.js ➔ ???   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 1
nop 1
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
1
/**
2
 * @file Module 'codingame/parsers/success'
3
 * @author woshilapin <[email protected]>
4
 */
5
/**
6
 * Parse successful response from Codingame when test pass.
7
 *
8
 * The typical response in this case if of the following form.
9
 * ```json
10
 * {
11
 * 	success: {
12
 * 		"output": output,
13
 * 		"comparison": {
14
 * 			"success": true
15
 * 		}
16
 * 	}
17
 * }
18
 * ```
19
 * @module codingame/parsers/success
20
 */
21
22
let name = `success`;
23
24
/**
25
 * Attempt to parse the body of a successful request to Codingame test API.
26
 * This function will try to map a response for a successful test.
27
 *
28
 * @function parse
29
 * @param {Object} body Body of the response
30
 * @returns {Promise<string>} Resolve with the successful result value
31
 * @throws {Error} Throw is parsing failed
32
 * @instance
33
 */
34
let parse = (body) => {
35
	try {
36
		let {
37
			"output": output,
38
			"comparison": {
39
				"success": success
40
			}
41
		} = body;
42
		if (success) {
0 ignored issues
show
Best Practice introduced by
If you intend to check if the variable success is declared in the current environment, consider using typeof success === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
43
			return Promise.resolve(output);
0 ignored issues
show
Bug introduced by
The variable output seems to be never declared. If this is a global, consider adding a /** global: output */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
44
		} else {
45
			throw `Success property must be true`;
46
		}
47
	} catch(error) {
48
		let message = `The body is not of response type '${name}'\n`;
49
		message += error.message;
50
		throw new Error(message);
51
	}
52
};
53
54
export default {
55
	"name": name,
56
	"parse": parse
57
};
58